home *** CD-ROM | disk | FTP | other *** search
/ Aminet 44 / Aminet 44 (2001)(GTI - Schatztruhe)[!][Aug 2001].iso / Aminet / dev / moni / systemviewer.lha / SysMemory.c < prev    next >
C/C++ Source or Header  |  2001-04-25  |  16KB  |  631 lines

  1. /****h* SysMemory.c [1.1] ******************************************
  2. * NAME
  3. *    SysMemory.c
  4. *
  5. * DESCRIPTION
  6. *    Display parameters associated with system memory.
  7. *
  8. *  GUI Designed by : Jim Steichen
  9. ********************************************************************
  10. *
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. #include <exec/types.h>
  17. #include <exec/execbase.h>
  18. #include <exec/memory.h>
  19.  
  20. #include <AmigaDOSErrs.h>
  21.  
  22. #include <intuition/intuition.h>
  23. #include <intuition/classes.h>
  24. #include <intuition/classusr.h>
  25. #include <intuition/imageclass.h>
  26. #include <intuition/gadgetclass.h>
  27.  
  28. #include <libraries/gadtools.h>
  29.  
  30. #include <graphics/displayinfo.h>
  31. #include <graphics/gfxbase.h>
  32.  
  33. #include <clib/exec_protos.h>
  34. #include <clib/intuition_protos.h>
  35. #include <clib/gadtools_protos.h>
  36. #include <clib/graphics_protos.h>
  37. #include <clib/utility_protos.h>
  38. #include <clib/diskfont_protos.h>
  39.  
  40. #include "CPGM:GlobalObjects/CommonFuncs.h"
  41.  
  42. #include "SysLists.h"
  43.  
  44. #define MLV      0
  45. #define Update   1
  46. #define Priority 2
  47. #define Cancel   3
  48. #define More     4
  49.  
  50. #define MEM_CNT  5
  51.  
  52. #define LISTVIEWGADGET MemGadgets[ MLV ]
  53.  
  54. PRIVATE char ver[] = "\0$VER: SysMemory 1.1 (25-Apr-2001) by J.T. Steichen\0";
  55.  
  56. PRIVATE struct Window       *MemWnd   = NULL;
  57. PRIVATE struct Gadget       *MemGList = NULL;
  58. PRIVATE struct IntuiMessage  MemMsg;
  59. PRIVATE struct Gadget       *MemGadgets[ MEM_CNT ];
  60.  
  61. PRIVATE UWORD MemLeft   = 0;
  62. PRIVATE UWORD MemTop    = 16;
  63. PRIVATE UWORD MemWidth  = 632;
  64. PRIVATE UWORD MemHeight = 228;
  65. PRIVATE UBYTE *MemWdt   = (UBYTE *) "System Memory Info:";
  66.  
  67. PRIVATE struct TextFont *MFont = NULL;
  68.  
  69. // -----------------------------------------------------------------
  70.  
  71. PRIVATE UBYTE *ttl = "Address   Pri Lower    Upper    Maximum In-Use"
  72.                      " Free   Largest Name";
  73.  
  74. #define MAXMEMS    10
  75. #define NODELENGTH 80
  76.  
  77. PRIVATE struct List MemList;
  78. PRIVATE struct Node MemNode;
  79. PRIVATE struct Node MemNodes[ MAXMEMS ]              = { NULL, };
  80. PRIVATE UBYTE       NodeStrs[ MAXMEMS * NODELENGTH ] = "";
  81.  
  82. PRIVATE struct ListViewMem lvm = { 0, };
  83.  
  84. // -----------------------------------------------------------------
  85.     
  86. PRIVATE UWORD MemGTypes[] = {
  87.  
  88.    LISTVIEW_KIND, BUTTON_KIND, BUTTON_KIND,
  89.    BUTTON_KIND,   BUTTON_KIND
  90. };
  91.  
  92. PRIVATE int MLVClicked(      int whichitem );
  93. PRIVATE int UpdateClicked(   int dummy     );
  94. PRIVATE int PriorityClicked( int dummy     );
  95. PRIVATE int CancelClicked(   int dummy     );
  96. PRIVATE int MoreClicked(     int dummy     );
  97.  
  98. PRIVATE struct NewGadget MemNGad[] = {
  99.  
  100.      2,   3, 627, 200,                 NULL, NULL, MLV, 
  101.    0, NULL, (APTR) MLVClicked,
  102.    
  103.      2, 205,  71,  17, (UBYTE *) "_Update",  NULL, Update, 
  104.    PLACETEXT_IN, NULL, (APTR) UpdateClicked,
  105.  
  106.    396, 205,  72,  17, (UBYTE *) "Priority", NULL, Priority, 
  107.    PLACETEXT_IN, NULL, (APTR) PriorityClicked,
  108.  
  109.    554, 205,  72,  17, (UBYTE *) "_Cancel",  NULL, Cancel, 
  110.    PLACETEXT_IN, NULL, (APTR) CancelClicked,
  111.  
  112.     82, 205,  71,  17, (UBYTE *) "_More",    NULL, More, 
  113.    PLACETEXT_IN, NULL, (APTR) MoreClicked
  114. };
  115.  
  116. PRIVATE ULONG MemGTags[] = {
  117.  
  118.    GTLV_ShowSelected, NULL, LAYOUTA_Spacing, 2, TAG_DONE,
  119.    GT_Underscore,      '_', TAG_DONE,
  120.    GA_Disabled,       TRUE, TAG_DONE,
  121.    GT_Underscore,      '_', TAG_DONE,
  122.    GT_Underscore,      '_', GA_Disabled, TRUE, TAG_DONE
  123.  
  124. };
  125.  
  126. /* -------------------------------------------------------------- */
  127.  
  128. PRIVATE struct MemHeader *GetMemoryHeader( ULONG address )
  129. {
  130.    IMPORT struct ExecBase *SysBase;
  131.  
  132.    struct MemHeader *addr = NULL;   
  133.    struct List      *lptr = NULL;
  134.  
  135.    Forbid();
  136.  
  137.      lptr  = &SysBase->MemList;
  138.      addr  = (struct MemHeader *) lptr->lh_Head;
  139.      
  140.      while ((addr != (struct MemHeader *) address) && (addr != NULL))
  141.         addr = (struct MemHeader *) addr->mh_Node.ln_Succ;
  142.  
  143.    Permit();
  144.  
  145.    if (addr != NULL)
  146.       return( addr );
  147.    else
  148.       return( NULL );
  149. }
  150.  
  151. PRIVATE ULONG GetMemoryLargest( int memtype )
  152. {
  153.    return( AvailMem( memtype | MEMF_LARGEST ) );
  154. }
  155.  
  156. PRIVATE ULONG GetMemorySize( int memtype )
  157. {
  158.    return( AvailMem( memtype | MEMF_TOTAL ) );
  159. }
  160.  
  161. PRIVATE int InitializeMemoryList( void )
  162. {
  163.    IMPORT struct ExecBase *SysBase;
  164.  
  165.    struct List      *lptr = &SysBase->MemList;
  166.    struct MemHeader *addr = (struct MemHeader *) lptr->lh_Head;   
  167.    struct MemHeader *mp   = GetMemoryHeader( (ULONG) addr );
  168.    ULONG             lrg  = GetMemoryLargest( addr->mh_Attributes );
  169.    ULONG             max  = 0L; 
  170.    ULONG             tmax = 0L, tuse = 0L, tfree = 0L;
  171.    int               i, rval = 0;
  172.       
  173.    HideListFromView( LISTVIEWGADGET, MemWnd );
  174.  
  175.    max = GetMemorySize( addr->mh_Attributes );
  176.  
  177.    for (i = 1; i <= MAXMEMS; i++)
  178.       {
  179.       // i = 1 so we don't overwrite the column headings.
  180.  
  181.       sprintf( &NodeStrs[i * NODELENGTH], 
  182.                "%08LX %4d %08LX %08LX %5uK %5uK  %5uK %5uK  %-20.20s",
  183.                mp, mp->mh_Node.ln_Pri, mp->mh_Lower, mp->mh_Upper,
  184.                max / 1000, 
  185.                (max - mp->mh_Free) / 1000, 
  186.                mp->mh_Free / 1000, 
  187.                lrg / 1000,
  188.                mp->mh_Node.ln_Name
  189.              );
  190.  
  191.       tmax  += max;
  192.       tuse  += max - mp->mh_Free;
  193.       tfree += mp->mh_Free;
  194.  
  195.       rval++;
  196.             
  197.       if (addr != NULL) 
  198.          {
  199.          addr = (struct MemHeader *) addr->mh_Node.ln_Succ;
  200.  
  201.          if (strcmp( "chip memory", addr->mh_Node.ln_Name ) == 0)
  202.             {
  203.             mp   = GetMemoryHeader( (ULONG) addr );
  204.             lrg  = GetMemoryLargest( addr->mh_Attributes );
  205.             max  = GetMemorySize( addr->mh_Attributes );
  206.  
  207.             sprintf( &NodeStrs[(i + 1) * NODELENGTH], 
  208.                      "%08LX %4d %08LX %08LX %5uK %5uK  %5uK %5uK  %-20.20s",
  209.                      mp, mp->mh_Node.ln_Pri, mp->mh_Lower, mp->mh_Upper,
  210.                      max / 1000, 
  211.                      (max - mp->mh_Free) / 1000, 
  212.                      mp->mh_Free / 1000, 
  213.                      lrg / 1000,
  214.                      mp->mh_Node.ln_Name
  215.                    );
  216.  
  217.             tmax  += max;
  218.             tuse  += max - mp->mh_Free;
  219.             tfree += mp->mh_Free;
  220.             rval++;
  221.                  
  222.             break; // chip memory MemHeader is the last valid MemHeader!
  223.             }
  224.  
  225.          mp   = GetMemoryHeader( (ULONG) addr );
  226.          lrg  = GetMemoryLargest( addr->mh_Attributes );
  227.          max  = GetMemorySize( addr->mh_Attributes );
  228.          }
  229.       else
  230.          break;
  231.       }
  232.  
  233.    if ((i + 2) < MAXMEMS)
  234.       sprintf( &NodeStrs[ NODELENGTH * (i + 2)], 
  235.                "                       TOTALS:  %5uK %5uK  %5uK",
  236.                tmax / 1000, tuse / 1000, tfree / 1000
  237.              );
  238.  
  239.    // Turn on the list view gadget again:   
  240.    GT_SetGadgetAttrs( LISTVIEWGADGET, MemWnd, NULL,
  241.                       GTLV_Labels,       &MemList,
  242.                       GTLV_Selected,     0, 
  243.                       GTLV_MaxPen,       255,
  244.                       GTLV_ItemHeight,   12,
  245.                       TAG_END
  246.                     );
  247.    return( rval );
  248. }
  249.  
  250. PRIVATE ULONG CurrentMem  = 0L;
  251. PRIVATE int   CurrentType = MEMF_CHIP;
  252.  
  253. PRIVATE char mt[80], *modtitle = &mt[0];
  254.  
  255. PRIVATE int MLVClicked( int whichitem )
  256. {
  257.    ULONG addr = 0L;
  258.  
  259.    if (whichitem == 0)
  260.       {
  261.       // Disable buttons because user selected the title item:
  262.  
  263.       GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  264.                          GA_Disabled, TRUE, TAG_DONE 
  265.                        );
  266.  
  267.       GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  268.                          GA_Disabled, TRUE, TAG_DONE 
  269.                        );
  270.    
  271.       DisplayTitle( MemWnd, MemWdt );
  272.  
  273.       CurrentMem = 0L;
  274.  
  275.       return( (int) TRUE );
  276.       }
  277.  
  278.    // Now get address from the item:
  279.    
  280.    (void) stch_l( MemNodes[ whichitem ].ln_Name, (long *) &addr );
  281.    CurrentMem = (ULONG) addr;
  282.  
  283.    if (addr == NULL)
  284.       {
  285.       GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  286.                          GA_Disabled, TRUE, TAG_DONE 
  287.                        );
  288.  
  289.       GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  290.                          GA_Disabled, TRUE, TAG_DONE 
  291.                        );
  292.    
  293.       SetWindowTitles( MemWnd, MemWdt, (UBYTE *) -1 );
  294.  
  295.       return( (int) TRUE );
  296.       }    
  297.    else
  298.       {
  299.       sprintf( modtitle, "%-45.45s  You Selected:  %08LX", MemWdt, addr );
  300.       DisplayTitle( MemWnd, modtitle );
  301.       }
  302.  
  303.    // Enable buttons because user sel'd a Memory Block from the ListView:
  304.  
  305.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  306.                       GA_Disabled, FALSE, TAG_DONE 
  307.                     );
  308.  
  309.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  310.                       GA_Disabled, FALSE, TAG_DONE 
  311.                     );
  312.  
  313.    return( (int) TRUE );
  314. }
  315.  
  316. PRIVATE int UpdateClicked( int dummy )
  317. {
  318.    int i;
  319.    
  320.    for (i = 1; i < MAXMEMS; i++)
  321.        NodeStrs[ i * NODELENGTH ] = '\0'; // Kill old ListView strings.
  322.  
  323.    // Disable buttons until user selects an item again:
  324.  
  325.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  326.                       GA_Disabled, TRUE, TAG_DONE 
  327.                     );
  328.  
  329.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  330.                       GA_Disabled, TRUE, TAG_DONE 
  331.                     );
  332.    
  333.    DisplayTitle( MemWnd, MemWdt );
  334.  
  335.    CurrentMem = 0L;
  336.  
  337.    if (InitializeMemoryList() < 0)
  338.       {
  339.       // No Memory Blocks known to SysBase!!
  340.       SetReqButtons( "OKAY" );
  341.  
  342.       (void) Handle_Problem( "No Memory Blocks found!", 
  343.                              "OS ERROR??", NULL
  344.                            );
  345.  
  346.       SetReqButtons( "CONTINUE|ABORT" );
  347.       }  
  348.  
  349.    GT_RefreshWindow( MemWnd, NULL );
  350.  
  351.    return( (int) TRUE );
  352. }
  353.  
  354. PRIVATE int PriorityClicked( int dummy )
  355. {
  356.    UserInfo( "NOT Implemented yet!", "User Information:" );
  357.    
  358.    return( (int) TRUE );
  359. }
  360.  
  361. PRIVATE void CloseMemWindow( void )
  362. {
  363.    if (MemWnd != NULL)
  364.       {
  365.       CloseWindow( MemWnd );
  366.       MemWnd = NULL;
  367.       }
  368.  
  369.    if (MemGList != NULL)
  370.       {
  371.       FreeGadgets( MemGList );
  372.       MemGList = NULL;
  373.       }
  374.  
  375.    if (MFont != NULL)
  376.       {
  377.       CloseFont( MFont );
  378.       MFont = NULL;
  379.       }
  380.  
  381.    return;
  382. }
  383.  
  384. PRIVATE int MemCloseWindow( void )
  385. {
  386.    CloseMemWindow();
  387.    return( (int) FALSE );
  388. }
  389.  
  390. PRIVATE int MoreClicked( int dummy )
  391. {
  392.    IMPORT int ViewMemory( int type, ULONG startaddress, ULONG endaddress );
  393.    
  394.    UserInfo( "NOT Implemented yet!", "USER Info:" );
  395.  
  396. /*
  397.    if (ViewMemory( CurrentType, CurrentMem, ???? ) < 0)
  398.       {
  399.       // User asked to abort in ViewMemory()!
  400.       return( MemCloseWindow() );
  401.       }
  402. */
  403.    return( (int) TRUE );
  404. }
  405.  
  406.  
  407. PRIVATE int CancelClicked( int dummy )
  408. {
  409.    return( MemCloseWindow() );
  410. }
  411.  
  412. PRIVATE BOOL MemVanillaKey( int whichkey )
  413. {
  414.    BOOL rval = TRUE;
  415.    
  416.    switch (whichkey)
  417.       {
  418.       case 'c':
  419.       case 'C':
  420.          rval = CancelClicked( 0 );
  421.          break;
  422.                
  423.       case 'u':
  424.       case 'U':
  425.          rval = UpdateClicked( 0 );
  426.          break;
  427.          
  428.       case 'm':
  429.       case 'M':
  430.          rval = MoreClicked( 0 );
  431.          break;
  432.       }
  433.   
  434.   return( rval );
  435. }
  436.  
  437.  
  438. PRIVATE int OpenMemWindow( void )
  439. {
  440.    struct NewGadget  ng;
  441.    struct Gadget    *g;
  442.    UWORD             lc, tc;
  443.    UWORD             wleft = MemLeft, wtop = MemTop, ww, wh;
  444.  
  445.    ComputeFont( Scr, Font, &CFont, MemWidth, MemHeight );
  446.  
  447.    ww = ComputeX( CFont.FontX, MemWidth );
  448.    wh = ComputeY( CFont.FontY, MemHeight );
  449.  
  450.    if ((wleft + ww + CFont.OffX + Scr->WBorRight) > Scr->Width)
  451.       wleft = Scr->Width - ww;
  452.  
  453.    if ((wtop + wh + CFont.OffY + Scr->WBorBottom) > Scr->Height)
  454.       wtop = Scr->Height - wh;
  455.  
  456.    if ( !(MFont = OpenDiskFont( Font )))
  457.       return( -5 );
  458.  
  459.    if ( !(g = CreateContext( &MemGList )))
  460.       return( -1 );
  461.  
  462.    for (lc = 0, tc = 0; lc < MEM_CNT; lc++)
  463.       {
  464.       CopyMem( (char *) &MemNGad[lc], (char *) &ng, 
  465.                (long) sizeof( struct NewGadget )
  466.              );
  467.  
  468.       ng.ng_VisualInfo = VisualInfo;
  469.       ng.ng_TextAttr   = Font;
  470.       ng.ng_LeftEdge   = CFont.OffX + ComputeX( CFont.FontX, 
  471.                                                 ng.ng_LeftEdge
  472.                                               );
  473.  
  474.       ng.ng_TopEdge    = CFont.OffY + ComputeY( CFont.FontY, 
  475.                                                 ng.ng_TopEdge
  476.                                               );
  477.  
  478.       ng.ng_Width      = ComputeX( CFont.FontX, ng.ng_Width );
  479.       ng.ng_Height     = ComputeY( CFont.FontY, ng.ng_Height );
  480.  
  481.       MemGadgets[lc] = g = CreateGadgetA( (ULONG) MemGTypes[lc], 
  482.                              g, 
  483.                              &ng, 
  484.                              (struct TagItem *) &MemGTags[tc] );
  485.  
  486.       while (MemGTags[tc] != NULL)
  487.          tc += 2;
  488.  
  489.       tc++;
  490.  
  491.       if (NOT g)
  492.          return( -2 );
  493.       }
  494.  
  495.    if ( !(MemWnd = OpenWindowTags( NULL,
  496.  
  497.                      WA_Left,        wleft,
  498.                      WA_Top,         wtop,
  499.                      WA_Width,       ww + CFont.OffX + Scr->WBorRight,
  500.                      WA_Height,      wh + CFont.OffY + Scr->WBorBottom,
  501.                      
  502.                      WA_IDCMP,       LISTVIEWIDCMP | BUTTONIDCMP 
  503.                        | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW
  504.                        | IDCMP_VANILLAKEY,
  505.                        
  506.                      WA_Flags,       WFLG_DRAGBAR | WFLG_DEPTHGADGET 
  507.                        | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH 
  508.                        | WFLG_ACTIVATE | WFLG_RMBTRAP,
  509.  
  510.                      WA_Gadgets,     MemGList,
  511.                      WA_Title,       MemWdt,
  512.                      WA_ScreenTitle, "System Info",
  513.                      TAG_DONE )) 
  514.       )
  515.       return( -4 );
  516.  
  517.    GT_RefreshWindow( MemWnd, NULL );
  518.  
  519.    return( 0 );
  520. }
  521.  
  522. PRIVATE int HandleMemIDCMP( void )
  523. {
  524.    struct IntuiMessage  *m;
  525.    int                 (*func)( int code );
  526.    BOOL                  running = TRUE;
  527.  
  528.    while (running == TRUE)
  529.       {
  530.       if ((m = GT_GetIMsg( MemWnd->UserPort )) == NULL)
  531.          {
  532.          (void) Wait( 1L << MemWnd->UserPort->mp_SigBit );
  533.          continue;
  534.          }
  535.  
  536.       CopyMem( (char *) m, (char *) &MemMsg, 
  537.                (long) sizeof( struct IntuiMessage )
  538.              );
  539.  
  540.       GT_ReplyIMsg( m );
  541.  
  542.       switch (MemMsg.Class)
  543.          {
  544.          case IDCMP_REFRESHWINDOW:
  545.             GT_BeginRefresh( MemWnd );
  546.             GT_EndRefresh( MemWnd, TRUE );
  547.             break;
  548.  
  549.          case IDCMP_CLOSEWINDOW:
  550.             running = MemCloseWindow();
  551.             break;
  552.  
  553.          case IDCMP_VANILLAKEY:
  554.             running = (int) MemVanillaKey( MemMsg.Code );
  555.             break;
  556.  
  557.          case IDCMP_GADGETUP:
  558.          case IDCMP_GADGETDOWN:
  559.             func = (void *) ((struct Gadget *)MemMsg.IAddress)->UserData;
  560.             if (func != NULL)
  561.                running = func( MemMsg.Code );
  562.             break;
  563.          }
  564.       }
  565.  
  566.    return( running );
  567. }
  568.  
  569. PUBLIC int HandleMemoryLV( void )
  570. {
  571.    int i = 0;
  572.  
  573.    if (SetupSystemList( &OpenMemWindow ) < 0)
  574.       {
  575.       (void) Handle_Problem( "Couldn't open a System ListViewer!", 
  576.                              "Allocation Problem:", NULL 
  577.                            );
  578.       return( -1 );
  579.       }
  580.  
  581.    // Disable buttons until user selects an item from the ListView:
  582.  
  583.    GT_SetGadgetAttrs( MemGadgets[ More ], MemWnd, NULL,
  584.                       GA_Disabled, TRUE, TAG_DONE 
  585.                     );
  586.  
  587.    GT_SetGadgetAttrs( MemGadgets[ Priority ], MemWnd, NULL,
  588.                       GA_Disabled, TRUE, TAG_DONE 
  589.                     );
  590.    
  591.    SetNotifyWindow( MemWnd ); // For Handle_Problem().
  592.   
  593.    lvm.lvm_NodeStrs   = &NodeStrs[0];
  594.    lvm.lvm_Nodes      = &MemNodes[0];
  595.    lvm.lvm_NumItems   = MAXMEMS;
  596.    lvm.lvm_NodeLength = NODELENGTH;
  597.  
  598.    SetupList( &MemList, &lvm );   
  599.  
  600.    strcpy( MemNodes[0].ln_Name, ttl );
  601.  
  602.    (void) InitializeMemoryList();
  603.  
  604.    GT_SetGadgetAttrs( LISTVIEWGADGET, MemWnd, NULL,
  605.                       GTLV_Labels,       &MemList,
  606.                       GTLV_Selected,     0, 
  607.                       GTLV_MaxPen,       255,
  608.                       GTLV_ItemHeight,   12,
  609.                       TAG_END
  610.                     );
  611.  
  612.    GT_RefreshWindow( MemWnd, NULL );
  613.  
  614.    (void) HandleMemIDCMP();
  615.    
  616.    ShutdownSystemList();
  617.    return( 0 );
  618. }
  619.  
  620. #ifdef DEBUG
  621.  
  622. PUBLIC int main( void )
  623. {
  624.    return( HandleMemoryLV() );
  625. }
  626.  
  627. #endif
  628.  
  629. /* --------------------- END of SysMemory.c file! --------------- */
  630.